package com.hc.util.net.ui;

import java.io.IOException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;

/**
 *
 * @author fenfei
 */
public class MessagePane extends JEditorPane {
    public MessagePane(){
        super();
        try {
            setPage(MessagePane.class.getResource("msgPane.html"));
        } catch (IOException ex) {
            Logger.getLogger(MessagePane.class.getName()).log(Level.SEVERE, null, ex);
        }
        
         setEditable(false);
        lang=ResourceBundle.getBundle("com/hc/util/net/ui/lang");
        this.setToolTipText(lang.getString("msgPaneTip"));
    }
    ResourceBundle lang;
       
    public void append(String text,String cls){
       try {
          HTMLDocument  doc=(HTMLDocument) getDocument();
         Element   body=doc.getElement(doc.getDefaultRootElement(), StyleConstants.NameAttribute, HTML.Tag.BODY);
            doc.insertBeforeEnd(body, "<p class='"+cls+"' >"+text+"</p>");
        } catch (BadLocationException ex) {
            Logger.getLogger(MessagePane.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MessagePane.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void appendInMsg(String msg){
        this.append(msg, "inMsg");
    }
    public void appendOutMsg(String msg){
        msg=msg.replace("\n", "<br>");
        this.append(msg, "outMsg");
    }
    public void appendInfo(String msg){
        append(msg,"info");
    }
    public void appendError(String msg){
        append(msg,"err");
    }
    
    public static void main(String[]arg) throws IOException{
   MessagePane ep=new MessagePane();
       
       JOptionPane.showMessageDialog(null, ep);
       System.out.println(ep.getText());
    }
}
